package dao;

import entity.Result;
import entity.Subject;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import untils.DruidUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author:SA
 * @DATA:DATA
 */


public class ResultDaoImpl implements ResultDao{


    @Override
    public Subject selectByName(String subName) {
        return null;
    }

    @Override
    public int addResult(String stuId, long subjectId, double garde) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into result(stuid,subid,grade)values(?,?,?)";
        try {
            int update = queryRunner.update(sql, stuId, subjectId, garde);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Result> selectAll(String subname) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "SELECT student.id,`subject`.id,result.grade,student.sname,`subject`.subname from result join student on student.id=result.stuid join `subject` on result.subid = `subject`.id where `subject`.subname = ?";

        try {
            List<Result> results = queryRunner.query(sql, new BeanListHandler<>(Result.class), subname);
            return results;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
